package no.ffi.gismoidm.attrex;

import java.util.Properties;

/**
 * Test if there is a substring occurence
 * @author anders
 */
public class Contains extends BooleanOperator {
    
    private StringOperand op1, op2;

    /* Is op2 a substring of op1? */
    public Contains(StringOperand op1, StringOperand op2) { 
        this.op1 = op1;
        this.op2 = op2;
    }

    // Utility method for short hand use
    public Contains(String s1, String s2) {
        this(new AttributeOperand(s1),new StringOperand(s2));
    }

    @Override
    public BooleanValue getBooleanValue(Properties attributes) throws AttributeEvaluationException {
        if (op1==null || op2==null) return new BooleanValue(false);
        String s1 = op1.getStringValue(attributes);
        String s2 = op2.getStringValue(attributes);
        if (s1==null || s2==null) return new BooleanValue(false);
        return new BooleanValue(s1.indexOf(s2)>=0);
    }

    public String toString() {
        return "contains(" + op1.toString() + "," + op2.toString() + ")";
    }
    
}
